myApp.controller('UCListController', function ($scope,$http,ngDialog) {

    $scope.info={};
    $scope.info.ucList=[];

    $scope.info.userName = getUserAccount();
    $scope.info.userRole = getUserRole();

      $scope.arrayContains = function (array,obj) {
        return arrayContains(array,obj);
    };

    $http.post(java_url+'/beetle/uCloud/getUCloudList', {
        tokenId:getTokenId()
    }).success(function (data) {
        angular.forEach(data.data.list,function (vm) {
            if(arrayContains($scope.info.userRole,'admin') || arrayContains($scope.info.userRole,'ops')
                || $scope.info.userName == vm.name
                || vm.name == 'AD_BUILD'){
                vm.modifiable=false;
                vm.createDateOpened=false;
                vm.returnDateOpened=false;
                $scope.info.ucList.push(vm);
            }
        });
    });

    $scope.editVm = function (vm) {
        vm.modifiable = !vm.modifiable;
        if(!vm.modifiable){
            $.ajax({
                type: "POST",
                url: java_url+"/beetle/uCloud/saveUCloudInfo?tokenId="+getTokenId(),
                data: JSON.stringify(vm),
                dataType : "JSON",
                contentType : "application/json; charset=utf-8",
                async:false,
                success: function(data){
                    console.log("editVm:"+JSON.stringify(vm)+",desc="+data.desc);
                }
            });
        };
    };

    $scope.goProgress = function (vm) {
        window.location.href="#/menu/ucProgress/"+vm.ip;
    };

    $scope.goRelease = function (vm) {
        window.location.href="#/menu/ucRelease/"+vm.name;
    };

    $scope.goInit = function (vm) {
        window.location.href="#/menu/ucInit/"+vm.ip;
    };

    $scope.checkDump = function (vm) {
        if(vm.isRunning){
            return;
        }
        openDialog(ngDialog,$scope,
            "检查数据库文件状态,将耗费些时间,是否继续?"
            ,true,function(flag){
                if(!flag){
                    return;
                }
                vm.isRunning = true;
                $http.get(getUCloudAddress(vm.ip)+"/database/check?data_from=demo",{
                    headers : {'Accept':'application/json'}
                }).success(function (data) {
                    vm.isRunning=false;
                    var name = '';
                    angular.forEach(data,function (dump) {
                        if(dump.dump_status != "文件状态正常"){
                            name += dump.name+","
                        }
                    });
                    if(name != ''){
                        openDialog(ngDialog,$scope,"存在异常数据库文件,"+name,false,function () {});
                    }else {
                        openDialog(ngDialog,$scope,"数据库文件状态全部正常!",false,function () {});
                    }
                }).error(function () {
                    vm.isRunning=false;
                    openDialog(ngDialog,$scope,"请求虚拟机异常,请联系管理员!",false,function () {});
                });
        });
    };

    $scope.updateDumpTime = function (vm) {
        if(vm.isRunning){
            return;
        }
        openDialog(ngDialog,$scope,
            "是否刷新数据库文件更新时间?"
            ,true,function(flag){
                if(!flag){
                    return;
                }
                vm.isRunning = true;
                $.ajax({
                    type: "put",
                    url: getUCloudAddress(vm.ip)+'/database/updateDumpTime?data_from=demo',
                    dataType : "JSON",
                    contentType : "application/json",
                    async:false,
                    success: function(){
                        vm.isRunning=false;
                        openDialog(ngDialog,$scope,"刷新数据库文件更新时间完成",false,function () {});
                    },
                    error:function () {
                        vm.isRunning=false;
                        openDialog(ngDialog,$scope,"请求虚拟机异常,请联系管理员!",false,function () {});
                    }
                });
            });
    };
});